<template>
	<view class="save-content">
		<view class="drag">
			<view class="canvas-body" :style="bodyStyle">
				<canvas :canvas-id="canvasId" :style="'width: ' + canvasSize + 'px; height: ' + canvasSize + 'px;'" />
			</view>
		</view>

		<view class="bottom-btn flex-center">
			<button open-type="share" style="width: 30%;height: 96rpx;background-color: transparent;border: 0px;"
				class="cu-btn radius margin-left text-white">
				<image src="../../static/assets/images/ic_share.png" style="width: 36rpx;height: 36rpx;" class="margin-right"/>分享
			</button>
			<view style="width: 40%;height: 96rpx;"></view>
			<view style="width: 30%;height: 96rpx;" class="cu-btn bg-green radius margin-right" @click="heIMg">保存图片
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				canvasId: "saveCanvas",
				canvasSize: '',
				bodyStyle: '',
			}
		},
		onLoad: function(option) {
			this.getSysInfo()
			const urls = JSON.parse(decodeURIComponent(option.url))
			const ctx = uni.createCanvasContext(this.canvasId)

			//绘制画布背景确定画布大小
			ctx.fillStyle = "#FFFFFF";
			ctx.fillRect(0, 0, this.canvasSize, this.canvasSize);

			//绘制背景图
			ctx.drawImage(urls[0], 0, 0, this.canvasSize, this.canvasSize);
			ctx.save();

			//绘制前景图
			ctx.drawImage(urls[1], 0, 0, this.canvasSize, this.canvasSize);
			ctx.restore();
			ctx.draw()

		},
		onShareAppMessage(res) {
			return {
				title: '一键制作个性头像',
				path: '/pages/index/index'
			}
		},
		methods: {
			getSysInfo() {
				try {
					const res = uni.getSystemInfoSync();
					this.canvasSize = res.windowWidth - 40;
				} catch (e) {
					this.canvasSize = 300;
				} finally {
					this.bodyStyle = `width:${this.canvasSize}px;height:${this.canvasSize+4}px`
				}
			},
			heIMg() {
				uni.canvasToTempFilePath({
					x: 0,
					y: 0,
					width: this.canvasSize,
					height: this.canvasSize,
					canvasId: this.canvasId,
					success: function(res) {
						uni.saveImageToPhotosAlbum({
							filePath: res.tempFilePath,
							success: function() {
								uni.showModal({
									showCancel: false,
									confirmText: '知道了',
									title: "图片已保存至相册",
								})
							}
						});
						console.log("合成图片=", res.tempFilePath)
					},
					fail: function(err) {
						console.log(">>>err=", err)
					}
				})
			},
			onShare() {
				uni.canvasToTempFilePath({
					x: 0,
					y: 0,
					width: this.canvasSize,
					height: this.canvasSize,
					canvasId: this.canvasId,
					success: function(res) {
						uni.saveImageToPhotosAlbum({
							filePath: res.tempFilePath,
							success: function() {
								uni.showModal({
									showCancel: false,
									confirmText: '知道了',
									title: "图片已保存至相册",
								})
							}
						});
						console.log("合成图片=", res.tempFilePath)
					},
					fail: function(err) {
						console.log(">>>err=", err)
					}
				})
			}
		}
	}
</script>


<style lang="scss" scoped>
	.save-content {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;
		background-color: $uni-bg-color;
	}

	.drag {
		width: 100%;
		display: flex;
		flex-direction: row;
		text-align: center;
		justify-content: center;
		align-items: center;
		margin: 64rpx 0;
	}

	.canvas-body {
		border: 2px solid #FFFFFF;
		border-radius: 4px;
		display: flex;
	}

	.bottom-btn {
		position: absolute;
		width: 100%;
		height: 10vh;
		display: flex;
		background-color: #333333;
		flex-direction: row;
		bottom: 0rpx;
	}
</style>
